package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.FreightForwardInfo;
import com.yy.jhl.ui.manager.adapter.BoxAdapter;
import com.yy.jhl.ui.manager.contract.FreightForwardContract;
import com.yy.jhl.ui.manager.presenter.FreightForwardPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 国内 国外货代
 * Created by libingn on 2017/12/11.
 */

public class FreightForwardActivity extends BaseActivity<FreightForwardPresenter> implements FreightForwardContract.View {
    TextView userTypeText;
    TextView userNameText;
    TextView userCompanyText;
    TextView userPhoneText;
    @BindView(R.id.box_list_view)
    ListView boxListView;
    @BindView(R.id.add_foreign_forwarder_btn)
    Button addForeignForwarderBtn;
    BoxAdapter boxAdapter;
    private int type = 1;//1:国内货代 2：国外货代 3:到货验收
    private String batchId = "";
    private String addressId = "";
    private String shipStatus = "";
    private String shipList = "";

    public static void startAction(Context mContext, int type, String batchId, String addressId) {
        Intent intent = new Intent(mContext, FreightForwardActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("batchId", batchId);
        intent.putExtra("addressId", addressId);
        mContext.startActivity(intent);
    }

    /**
     * 国内货代专用
     * @param mContext
     * @param type
     * @param batchId
     * @param addressId
     * @param shipStatus
     */
    public static void startAction(Context mContext, int type, String batchId, String addressId,String shipStatus,String shipList) {
        Intent intent = new Intent(mContext, FreightForwardActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("batchId", batchId);
        intent.putExtra("addressId", addressId);
        intent.putExtra("shipStatus", shipStatus);
        intent.putExtra("shipList", shipList);
        mContext.startActivity(intent);
    }

    private void addHeadView() {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.header_act_freight_forward, null);
        userTypeText = (TextView) headerView.findViewById(R.id.user_type_text);
        userNameText = (TextView) headerView.findViewById(R.id.user_name_text);
        userCompanyText = (TextView) headerView.findViewById(R.id.user_company_text);
        userPhoneText = (TextView) headerView.findViewById(R.id.user_phone_text);
        boxListView.addHeaderView(headerView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_freight_forward;
    }

    @Override
    public void initView() {
        addHeadView();
        type = getIntent().getIntExtra("type", 1);
        batchId = getIntent().getStringExtra("batchId");
        addressId = getIntent().getStringExtra("addressId");
        shipStatus = getIntent().getStringExtra("shipStatus");
        shipList = getIntent().getStringExtra("shipList");
        String title = "";
        switch (type) {
            case 1:
                title = "国内货代";
                if (!StringUtil.isEmpty(shipStatus) && Integer.parseInt(shipStatus) <= 9){
                    addForeignForwarderBtn.setVisibility(View.VISIBLE);
                }else {
                    addForeignForwarderBtn.setVisibility(View.GONE);
                }
                break;
            case 2:
                title = "国外货代";
                addForeignForwarderBtn.setVisibility(View.GONE);
                break;
            case 3:
                title = "到货验收";
                addForeignForwarderBtn.setVisibility(View.GONE);
                break;
        }
        showTitle(title, new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        boxAdapter = new BoxAdapter(mContext);
        boxListView.setAdapter(boxAdapter);
        mPresenter.getList(batchId, addressId, true);
    }

    @Override
    public void getListSuccess(FreightForwardInfo info) {
        FreightForwardInfo.AccountBean account = info.getAccount();
        if (account != null) {
            userTypeText.setText(account.getType());
            userNameText.setText(account.getContact());
            userCompanyText.setText(account.getCompany_name());
            userPhoneText.setText(account.getPhone());
        }
        if (info.getPackageX() != null && info.getPackageX().getData() != null && info.getPackageX().getData().size() > 0) {
            boxAdapter.addAllData(info.getPackageX().getData());
            StringUtil.setListViewHeightBasedOnChildren(boxListView);
        }
    }

    @Override
    public void getListFail() {

    }
    @OnClick(R.id.add_foreign_forwarder_btn)
    public void onViewClicked() {
        ForwarderWebViewActivity.startAction(mContext,2,batchId,Integer.parseInt(shipStatus),shipList,1000);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1000:
                    String result =  data.getStringExtra("result");
                    finish();
                    break;
            }
        }
    }
}
